const search = {
    namespaced: true,
    state: {
        queryRes_movie: [],
        queryRes_book: [],
        queryRes_music: [],
        skip: 5
    },

    mutations: {
        query (state, payload) {
            switch (payload.tag) {
                case 'movie': 
                    state.queryRes_movie = payload.result;
                    break;
                case 'book': 
                    state.queryRes_book = payload.result;
                    break;
                case 'music': 
                    state.queryRes_music = payload.result;
                    break;
                default:
                    break;
            }
        },
        loadMore (state, payload) {
            state.skip += 5;
            let addSubjects = payload.result.subjects;
            let oldSubjects = state.queryRes_movie.subjects
            state.queryRes_movie.subjects = oldSubjects.concat(addSubjects);
        }
    },

    getters: {
        
    },

    actions: {
        query ({commit, state, rootGetters}, payload) {
            let $axios = rootGetters.$ax
            return  $axios.get('/v2/movie/search', {
                params: {
                    q: payload.queryStr,
                    count: 5
                }
            }).then(function(result){
                console.log('result.length->', result.data.subjects.length);
                commit('query', {
                    tag: 'movie',
                    result: result.data
                })
            }).catch(function(err){
                console.log(err);
            })


            // $axios.get('/v2/book/search', {
            //     params: {
            //         q: payload.queryStr,
            //         count: 8
            //     }
            // })
            // .then((result) => {
            //     commit('query', {
            //         tag: 'book',
            //         result: result.data
            //     })
            // }).catch((err) => {
            //     console.log(err)
            // })

            // $axios.get('/v2/music/search', {
            //     params: {
            //         q: payload.queryStr,
            //         count: 8
            //     }
            // })
            // .then((result) => {
            //     commit({
            //         type: 'query',
            //         tag: 'music',
            //         result: result.data
            //     })
            // }).catch((err) => {
            //     console.log(err)
            // })
        },

        // 获取更多
        loadMore ({commit, state, rootGetters}, payload) {
            let $axios = rootGetters.$ax;
            return $axios.get('/v2/movie/search', {
                params: {
                    q: payload.queryStr,
                    count: 5,
                    start: state.skip
                }
            }).then(function(result){
                commit({
                    type: 'loadMore',
                    result: result.data
                })
            }).catch(function(err){
                console.log(err);
            })
        }

    }
}

export default search;